package com.zbf.auth.dao;

import com.zbf.auth.user.Menu;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 作者: LCG
 * 日期: 2020/6/1 15:51
 * 描述:
 */
public interface MenuDao extends JpaRepository<Menu,Long> {

    /**
     * 获取权限菜单列表(获取1级菜单)
     * @param userRole
     * @return
     */
    @Query(value = "select DISTINCTROW bm.* from base_role_menu brm INNER JOIN base_menu bm on brm.menu_id=bm.id where brm.role_id in(:userRole) and bm.leval=1 order by bm.menuName desc",nativeQuery = true)
    public List<Menu> getMenuList(@Param("userRole") List<Long> userRole);


    /**
     * 根据角色Id，父菜单Id，和菜单等级查询菜单信息
     * @param userRole
     * @param parentCode
     * @param leval
     * @return
     */
    @Query(value = "select DISTINCTROW bm.* from base_role_menu brm INNER JOIN base_menu bm on brm.menu_id=bm.id where brm.role_id in (:userRole) and bm.leval= :leval and bm.parentCode= :parentCode order by bm.menuName desc",nativeQuery = true)
    public List<Menu> getChildMenuList(@Param("userRole") List<Long> userRole,@Param("parentCode") Long parentCode,@Param("leval") int leval);


    /**
     * 查询所有的一级菜单
     * @return
     */
    @Query(value = "select bm.* from  base_menu bm where bm.leval=1 order by bm.menuName desc",nativeQuery = true)
    public List<Menu> getAllMenuOfFirst();


    /**
     * 根据parentCode和leval查询子菜单
     * @param parentCode
     * @param leval
     * @return
     */
    @Query(value = "select bm.* from base_menu bm  where bm.leval= :leval and bm.parentCode= :parentCode order by bm.menuName desc",nativeQuery = true)
    public List<Menu> getChildrenListUsePraentCode(@Param("parentCode") Long parentCode,@Param("leval") int leval);

}
